package com.ctshk.rpc.ship.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ctshk.rpc.ship.dto.ShipProductPageDTO;
import com.ctshk.rpc.ship.dto.ShipProductSaleStrategyDTO;
import com.ctshk.rpc.ship.entity.ShipProductSaleStrategy;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ctshk.rpc.ship.req.ShipProductPageReq;
import com.ctshk.rpc.ship.req.ShipProductSaleStrategyQueryReq;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 船票销售加幅策略 Mapper 接口
 * </p>
 *
 * @author 刘蕾
 * @since 2021-03-10
 */
public interface ShipProductSaleStrategyMapper extends BaseMapper<ShipProductSaleStrategy> {


    /**
     * 获取船票列表
     *
     * @param req
     * @param data
     * @return
     */
    @Select("<script>" + "select t1.id,t1.code,t1.shelves_status shelvesStatus,t1.dep_city_name as depCityName ,t1.arr_city_name as arrCityName,(case t1.company_type when '1' then '信德船票' when '2' then '珠江船票' end )as companyType,t1.voyage\n" +
            ",t1.cabin_class_name as cabinClass,IFNULL(substring(t1.dep_time,12,5),t1.dep_time) depTime,t1.arr_time as arrTime,t2.dep_place_name as depPlaceName\n" +
            ",t2.arr_place_name as arrPlaceName,t1.ship_type_name as shipTypeName,(case t1.product_source when '1' then '手工產品' when '2' then 'API產品' end )as productSource,t1.supplier_name as supplierName,t1.applicable_crowd_name as applicableCrowdName,t1.charge_user_id as chargeUserId,t1.charge_user_name as chargeUser,\n" +
            "(case t1.sell_status when '1' then '有效' when '0' then '無效' end )as sellStatus,t1.pack_status as packStatus,t1.approval_status as approvalStatus\n" +
            ",t1.is_recommend as isRecommend \n" +
            "from t_ship_product t1 \n" +
            "left join t_ship_route t2 on t1.route_id = t2.id \n" +
            "WHERE t1.is_deleted = 0 " +
            "<if test=\"req.companyType != null and req.companyType != ''\">AND t1.company_type = #{req.companyType}</if> \n" +
            "<if test=\"req.voyage != null and req.voyage != ''\">AND t1.voyage LIKE CONCAT('%',#{req.voyage},'%')</if> \n" +
            "<if test=\"req.depCityId != null and req.depCityId != ''\">AND t1.dep_city_id = #{req.depCityId}</if> \n" +
            "<if test=\"req.arrCityId != null and req.arrCityId != ''\">AND t1.arr_city_id = #{req.arrCityId}</if> \n" +

            "<if test=\"req.depPlaceId != null and req.depPlaceId != ''\">AND t2.dep_place_id = #{req.depPlaceId}</if> \n" +
            "<if test=\"req.arrPlaceId != null and req.arrPlaceId != ''\">AND t2.arr_place_id = #{req.arrPlaceId}</if> \n" +

            " <if test='data != null and data.size > 0'>" +
            " AND (t1.charge_user_id IN \n" +
            " <foreach collection='data' item='userId' open='(' separator=',' close=')'>"+
            " #{userId}"+
            " </foreach> or t1.create_id IN \n"+
            " <foreach collection='data' item='userId' open='(' separator=',' close=')'>"+
            " #{userId}"+
            " </foreach> )\n"+
            " </if>" +

            "<if test=\"req.depTimeStart != null  \">AND t1.dep_time between #{req.depTimeStart} and #{req.depTimeEnd} </if> \n" +
            "<if test=\"req.arrTimeStart != null \">AND t1.arr_time between #{req.arrTimeStart} and #{req.arrTimeEnd} </if> \n" +
            "<if test=\"req.cabinClass != null and req.cabinClass != ''\">AND t1.cabin_class = #{req.cabinClass}</if> \n" +
            "<if test=\"req.productSource != null and req.productSource != ''\">AND t1.product_source = #{req.productSource}</if> \n" +
            "<if test=\"req.isRecommend != null and req.isRecommend != ''\">AND t1.is_recommend = #{req.isRecommend}</if> \n" +
            "<if test=\"req.shelvesStatus != null and req.shelvesStatus != ''\">AND t1.shelves_status = #{req.shelvesStatus}</if> \n" +
            "<if test=\"req.supplierName != null and req.supplierName != ''\">AND t1.supplier_name LIKE CONCAT('%',#{req.supplierName },'%')</if> \n" +
            "order BY t1.gmt_modified desc " + "</script>")
    IPage<ShipProductPageDTO> queryList(@Param("req") ShipProductPageReq req, IPage<ShipProductPageDTO> agentPage, @Param("data") List<Long> data);

    /**
     * 根据产品ID和日期获取船票信息
     *
     * @param req
     * @return
     */
    @Select("<script>" +
            "SELECT  t.id,t.name,t.product_id as productId  FROM t_ship_product_sale_strategy t "+
            " LEFT JOIN t_ship_product_sale_strategy_vaild_date t1 ON t.id = t1.strategy_id "+
            " where t.is_deleted = 0  "+
            "<if test=\"req.id != null and req.id != ''\"> AND t.product_id = #{req.id}</if> \n" +
            "<if test=\"req.queryDate != null \"> AND t1.time_begin &lt;=#{req.queryDate} and t1.time_end &gt;=#{req.queryDate}</if> \n" +
            "</script>")
    ShipProductSaleStrategyDTO querySaleStrategy(@Param("req") ShipProductSaleStrategyQueryReq req);
}
